package com.chinadci.smartcommunitysystembackstage.controller;


import com.chinadci.smartcommunitysystembackstage.entity.ScsAdmin;
import com.chinadci.smartcommunitysystembackstage.service.ScsActivityService;
import com.chinadci.smartcommunitysystembackstage.service.ScsAdminService;
import com.chinadci.smartcommunitysystembackstage.util.JWTUtils;
import com.chinadci.smartcommunitysystembackstage.util.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@RestController
@CrossOrigin
@Api("建业E家")
public class ScsAdminController {
    @Autowired
    private ScsAdminService service;
    //管理员登陆
    @ApiOperation(value = "管理员登录")           //swagger
    @ApiImplicitParam(name = "scsadmin",value = "对象(电话和密码)")       //表明要传入的参数
    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public Map Login(ScsAdmin scs){
        Map<String,Object> map=new HashMap<>();
        ScsAdmin admin=service.Login(scs);
        Map<String,String> usermap=new HashMap<>();
        if (admin!=null){
            usermap.put("aId",admin.getAId().toString());
            usermap.put("aName", admin.getAName());
            map.put("token", JWTUtils.GetToken(usermap));
            System.out.println(JWTUtils.GetToken(usermap));
        }
        map.put("data",admin);
        return map;
    }
    //添加管理员
    @ApiOperation(value = "添加管理员")           //swagger
    @ApiImplicitParam(name = "scsadmin",value = "对象")       //表明要传入的参数
    @RequestMapping(value = "/user/add",method = RequestMethod.POST)
    public Map Add(ScsAdmin scs){
        Map<String,Integer> map=new HashMap<>();
        if (service.Zc(scs)!=null){
            map.put("phone",1);
        }else{
            map.put("data",service.Add(scs));
            map.put("phone",0);
        }
        return map;
    }
    //查找所有管理员
    @ApiOperation(value = "查询所有管理员")           //swagger
    @ApiImplicitParam(name = "name",value = "管理员姓名")       //表明要传入的参数
    @RequestMapping(value = "/api/select",method = RequestMethod.GET)
    public Map Select(String name){
        Map<String,Object> map=new HashMap<>();
        map.put("data",service.Find(name));
        return map;
    }

    //删除指定管理员
    @ApiOperation(value = "删除指定管理员")           //swagger
    @ApiImplicitParam(name = "id",value = "管理员id")       //表明要传入的参数
    @RequestMapping(value = "/api/del",method = RequestMethod.POST)
    public Map Del(int id){
        Map<String,Object> map=new HashMap<>();
        map.put("data",service.Del(id));
        return map;
    }

    //修改指定管理员
    @ApiOperation(value = "修改指定管理员")           //swagger
    @ApiImplicitParam(name = "scsadmin",value = "对象")       //表明要传入的参数
    @RequestMapping(value = "/api/update",method = RequestMethod.POST)
    public Map Del(ScsAdmin scs){
        Map<String,Object> map=new HashMap<>();
        map.put("data",service.Update(scs));
        return map;
    }

    //修改管理员信息查询
    @ApiOperation(value = "修改管理员信息查询")           //swagger
    @ApiImplicitParam(name = "id",value = "管理员编号")       //表明要传入的参数
    @RequestMapping(value = "/api/find",method = RequestMethod.POST)
    public Map Finds(int id){
        Map<String,Object> map=new HashMap<>();
        map.put("data",service.Find(id));
        return map;
    }
}